package com.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blog.common.Result;
import com.blog.pojo.Thumb;
import com.blog.service.ThumbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 点赞相关
 */
@RestController
@RequestMapping("/thumb")
public class ThumbController {

    @Autowired
    private ThumbService thumbService;

    /**
     * 点赞或取消点赞
     *
     * @param userId 用户id
     * @param blogId 博客id
     * @return Result<String>
     */
    @GetMapping("/thumbsUpOrCancel")
    public Result<String> thumbsUpOrCancel(Long userId, Long blogId) {
        LambdaQueryWrapper<Thumb> thumbLambdaQueryWrapper = new LambdaQueryWrapper<>();
        thumbLambdaQueryWrapper.eq(Thumb::getUserId, userId).eq(Thumb::getBlogId, blogId);
        Thumb thumbServiceOne = thumbService.getOne(thumbLambdaQueryWrapper);
        if (thumbServiceOne == null) {//该user对该blog为未点赞状态
            Thumb thumb = new Thumb();
            thumb.setUserId(userId);
            thumb.setBlogId(blogId);
            thumbService.save(thumb);
            return Result.success("点赞成功！");
        }
        //该user对该blog为已经点过赞
        thumbService.remove(thumbLambdaQueryWrapper);
        return Result.success("点赞已取消！");
    }

    /**
     * 判断是否已经点赞
     * @param userId 用户id
     * @param blogId 博客id
     * @return Result<Boolean>
     */
    @GetMapping("/isOrNot")
    private Result<Boolean> isOrNot(Long userId, Long blogId){
        LambdaQueryWrapper<Thumb> thumbLambdaQueryWrapper = new LambdaQueryWrapper<>();
        thumbLambdaQueryWrapper.eq(Thumb::getUserId, userId).eq(Thumb::getBlogId, blogId);
        Thumb thumbServiceOne = thumbService.getOne(thumbLambdaQueryWrapper);
        if (thumbServiceOne == null)//该user对该blog为未点赞状态
            return Result.success(false);
        else //该user对该blog为已经点赞状态
            return Result.success(true);
    }
}
